@@ -1,5 +1,24 @@ |
||
1 | 1 |
module JsonPathOptionsOverwritable |
2 | 2 |
extend ActiveSupport::Concern |
3 |
+ # Using this concern allows providing optionional `<attribute>_path` options hash |
|
4 |
+ # attributes which will then (if not blank) be interpolated using the provided JSONPath. |
|
5 |
+ # |
|
6 |
+ # Example options Hash: |
|
7 |
+ # { |
|
8 |
+ # name: 'Huginn', |
|
9 |
+ # name_path: '$.name', |
|
10 |
+ # title: 'Hello from Huginn' |
|
11 |
+ # title_path: '' |
|
12 |
+ # } |
|
13 |
+ # Example event payload: |
|
14 |
+ # { |
|
15 |
+ # name: 'dynamic huginn' |
|
16 |
+ # } |
|
17 |
+ # calling agent.merge_json_path_options(event) returns the fowolling hash: |
|
18 |
+ # { |
|
19 |
+ # name: 'dynamic huggin' |
|
20 |
+ # title: 'Hello from Huginn' |
|
21 |
+ # } |
|
3 | 22 |
|
4 | 23 |
private |
5 | 24 |
def merge_json_path_options(event) |
@@ -0,0 +1,15 @@ |
||
1 |
+module WorkingHelpers |
|
2 |
+ extend ActiveSupport::Concern |
|
3 |
+ |
|
4 |
+ def event_created_within?(days) |
|
5 |
+ last_event_at && last_event_at > days.to_i.days.ago |
|
6 |
+ end |
|
7 |
+ |
|
8 |
+ def recent_error_logs? |
|
9 |
+ last_event_at && last_error_log_at && last_error_log_at > (last_event_at - 2.minutes) |
|
10 |
+ end |
|
11 |
+ |
|
12 |
+ def received_event_without_error? |
|
13 |
+ (last_receive_at.present? && last_error_log_at.blank?) || (last_receive_at.present? && last_error_log_at.present? && last_receive_at > last_error_log_at) |
|
14 |
+ end |
|
15 |
+end |
@@ -11,6 +11,7 @@ class Agent < ActiveRecord::Base |
||
11 | 11 |
include MarkdownClassAttributes |
12 | 12 |
include JSONSerializedField |
13 | 13 |
include RDBMSFunctions |
14 |
+ include WorkingHelpers |
|
14 | 15 |
|
15 | 16 |
markdown_class_attributes :description, :event_description |
16 | 17 |
|
@@ -83,18 +84,6 @@ class Agent < ActiveRecord::Base |
||
83 | 84 |
raise "Implement me in your subclass" |
84 | 85 |
end |
85 | 86 |
|
86 |
- def event_created_within?(days) |
|
87 |
- last_event_at && last_event_at > days.to_i.days.ago |
|
88 |
- end |
|
89 |
- |
|
90 |
- def recent_error_logs? |
|
91 |
- last_event_at && last_error_log_at && last_error_log_at > (last_event_at - 2.minutes) |
|
92 |
- end |
|
93 |
- |
|
94 |
- def received_event_without_error? |
|
95 |
- (last_receive_at.present? && last_error_log_at.blank?) || (last_receive_at.present? && last_error_log_at.present? && last_receive_at > last_error_log_at) |
|
96 |
- end |
|
97 |
- |
|
98 | 87 |
def create_event(attrs) |
99 | 88 |
if can_create_events? |
100 | 89 |
events.create!({ |
@@ -48,7 +48,6 @@ module Agents |
||
48 | 48 |
response = HTTParty.post "https://api.pushbullet.com/api/pushes", query_options(event) |
49 | 49 |
log(response.body) if response.body.include? 'error' |
50 | 50 |
end |
51 |
- save! |
|
52 | 51 |
end |
53 | 52 |
|
54 | 53 |
private |
@@ -1,6 +1,9 @@ |
||
1 | 1 |
require 'spec_helper' |
2 |
+require 'models/concerns/working_helpers' |
|
2 | 3 |
|
3 | 4 |
describe Agent do |
5 |
+ it_behaves_like WorkingHelpers |
|
6 |
+ |
|
4 | 7 |
describe ".run_schedule" do |
5 | 8 |
before do |
6 | 9 |
Agents::WeatherAgent.count.should > 0 |
@@ -565,56 +568,6 @@ describe Agent do |
||
565 | 568 |
end |
566 | 569 |
end |
567 | 570 |
|
568 |
- describe "recent_error_logs?" do |
|
569 |
- it "returns true if last_error_log_at is near last_event_at" do |
|
570 |
- agent = Agent.new |
|
571 |
- |
|
572 |
- agent.last_error_log_at = 10.minutes.ago |
|
573 |
- agent.last_event_at = 10.minutes.ago |
|
574 |
- agent.recent_error_logs?.should be_true |
|
575 |
- |
|
576 |
- agent.last_error_log_at = 11.minutes.ago |
|
577 |
- agent.last_event_at = 10.minutes.ago |
|
578 |
- agent.recent_error_logs?.should be_true |
|
579 |
- |
|
580 |
- agent.last_error_log_at = 5.minutes.ago |
|
581 |
- agent.last_event_at = 10.minutes.ago |
|
582 |
- agent.recent_error_logs?.should be_true |
|
583 |
- |
|
584 |
- agent.last_error_log_at = 15.minutes.ago |
|
585 |
- agent.last_event_at = 10.minutes.ago |
|
586 |
- agent.recent_error_logs?.should be_false |
|
587 |
- |
|
588 |
- agent.last_error_log_at = 2.days.ago |
|
589 |
- agent.last_event_at = 10.minutes.ago |
|
590 |
- agent.recent_error_logs?.should be_false |
|
591 |
- end |
|
592 |
- end |
|
593 |
- |
|
594 |
- describe "received_event_without_error?" do |
|
595 |
- before do |
|
596 |
- @agent = Agent.new |
|
597 |
- end |
|
598 |
- |
|
599 |
- it "should return false until the first event was received" do |
|
600 |
- @agent.received_event_without_error?.should == false |
|
601 |
- @agent.last_receive_at = Time.now |
|
602 |
- @agent.received_event_without_error?.should == true |
|
603 |
- end |
|
604 |
- |
|
605 |
- it "should return false when the last error occured after the last received event" do |
|
606 |
- @agent.last_receive_at = Time.now - 1.minute |
|
607 |
- @agent.last_error_log_at = Time.now |
|
608 |
- @agent.received_event_without_error?.should == false |
|
609 |
- end |
|
610 |
- |
|
611 |
- it "should return true when the last received event occured after the last error" do |
|
612 |
- @agent.last_receive_at = Time.now |
|
613 |
- @agent.last_error_log_at = Time.now - 1.minute |
|
614 |
- @agent.received_event_without_error?.should == true |
|
615 |
- end |
|
616 |
- end |
|
617 |
- |
|
618 | 571 |
describe "scopes" do |
619 | 572 |
describe "of_type" do |
620 | 573 |
it "should accept classes" do |
@@ -2,7 +2,7 @@ require 'spec_helper' |
||
2 | 2 |
require 'models/concerns/json_path_options_overwritable' |
3 | 3 |
|
4 | 4 |
describe Agents::HipchatAgent do |
5 |
- it_behaves_like 'JsonPathOptionsOverwritable' |
|
5 |
+ it_behaves_like JsonPathOptionsOverwritable |
|
6 | 6 |
|
7 | 7 |
before(:each) do |
8 | 8 |
@valid_params = { |
@@ -2,7 +2,7 @@ require 'spec_helper' |
||
2 | 2 |
require 'models/concerns/json_path_options_overwritable' |
3 | 3 |
|
4 | 4 |
describe Agents::PushbulletAgent do |
5 |
- it_behaves_like 'JsonPathOptionsOverwritable' |
|
5 |
+ it_behaves_like JsonPathOptionsOverwritable |
|
6 | 6 |
|
7 | 7 |
before(:each) do |
8 | 8 |
@valid_params = { |
@@ -1,6 +1,6 @@ |
||
1 | 1 |
require 'spec_helper' |
2 | 2 |
|
3 |
-shared_examples_for 'JsonPathOptionsOverwritable' do |
|
3 |
+shared_examples_for JsonPathOptionsOverwritable do |
|
4 | 4 |
before(:each) do |
5 | 5 |
@valid_params = described_class.new.default_options |
6 | 6 |
|
@@ -0,0 +1,53 @@ |
||
1 |
+require 'spec_helper' |
|
2 |
+ |
|
3 |
+shared_examples_for WorkingHelpers do |
|
4 |
+ describe "recent_error_logs?" do |
|
5 |
+ it "returns true if last_error_log_at is near last_event_at" do |
|
6 |
+ agent = Agent.new |
|
7 |
+ |
|
8 |
+ agent.last_error_log_at = 10.minutes.ago |
|
9 |
+ agent.last_event_at = 10.minutes.ago |
|
10 |
+ agent.recent_error_logs?.should be_true |
|
11 |
+ |
|
12 |
+ agent.last_error_log_at = 11.minutes.ago |
|
13 |
+ agent.last_event_at = 10.minutes.ago |
|
14 |
+ agent.recent_error_logs?.should be_true |
|
15 |
+ |
|
16 |
+ agent.last_error_log_at = 5.minutes.ago |
|
17 |
+ agent.last_event_at = 10.minutes.ago |
|
18 |
+ agent.recent_error_logs?.should be_true |
|
19 |
+ |
|
20 |
+ agent.last_error_log_at = 15.minutes.ago |
|
21 |
+ agent.last_event_at = 10.minutes.ago |
|
22 |
+ agent.recent_error_logs?.should be_false |
|
23 |
+ |
|
24 |
+ agent.last_error_log_at = 2.days.ago |
|
25 |
+ agent.last_event_at = 10.minutes.ago |
|
26 |
+ agent.recent_error_logs?.should be_false |
|
27 |
+ end |
|
28 |
+ end |
|
29 |
+ describe "received_event_without_error?" do |
|
30 |
+ before do |
|
31 |
+ @agent = Agent.new |
|
32 |
+ end |
|
33 |
+ |
|
34 |
+ it "should return false until the first event was received" do |
|
35 |
+ @agent.received_event_without_error?.should == false |
|
36 |
+ @agent.last_receive_at = Time.now |
|
37 |
+ @agent.received_event_without_error?.should == true |
|
38 |
+ end |
|
39 |
+ |
|
40 |
+ it "should return false when the last error occured after the last received event" do |
|
41 |
+ @agent.last_receive_at = Time.now - 1.minute |
|
42 |
+ @agent.last_error_log_at = Time.now |
|
43 |
+ @agent.received_event_without_error?.should == false |
|
44 |
+ end |
|
45 |
+ |
|
46 |
+ it "should return true when the last received event occured after the last error" do |
|
47 |
+ @agent.last_receive_at = Time.now |
|
48 |
+ @agent.last_error_log_at = Time.now - 1.minute |
|
49 |
+ @agent.received_event_without_error?.should == true |
|
50 |
+ end |
|
51 |
+ end |
|
52 |
+ |
|
53 |
+end |